package br.com.cast.scc.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.wicket.Session;

import br.com.cast.scc.model.IEntidade;

public class DAOBase<T extends IEntidade> {

	@PersistenceContext(unitName="persistencia")
	private EntityManager entityManager;
	
	public void salvar(T entidade){
		getEntityManager().persist(entidade);
	}
	
	public void alterar(T entidade){
		getEntityManager().merge(entidade);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<T> consultaTodos(Class<T> classe) {
		
		String hql = "FROM " + classe.getName();
		
		return getEntityManager().createQuery(hql).getResultList();
	}
	
	
	
	
	/*
	 * METODOS CRIA CONEXAO
	 */
	protected EntityManager getEntityManager(){
		return this.entityManager;
	}
	
	protected Session getHibernateCurrentSession(){
		return (Session) getEntityManager().getDelegate();
	}
	

}
